//
// Created by bill3000 on 2/11/21.
//

#ifndef INTELLI_MECHANIC_HPP
#define INTELLI_MECHANIC_HPP

#include "../mq/queue.hpp"

enum MechMacros {
    MECH_START_CONVEYER = 0x01,
    MECH_STOP_CONVEYER = 0x02,
    MECH_PUSH_PUSHER = 0x03,
    MECH_RESET_MECHANIC_SYSTEM = 0x04
};

class Mechanic {
public:
    static void start_mechanical_thread();
    static void wait_mechanical_thread();
    static void stop_mechanical_thread();
    static void dispose();

    Mechanic();
    ~Mechanic() = default;

private:
    Queue *mq;
    int fd;

    static Mechanic *mech;

    static pthread_t mechanical_demo_server_thread_id;
    static pthread_t mechanical_control_thread_id;
    static pthread_t mechanical_emergency_thread_id;

    static void* mechanical_control_thread(void *data);
    static void* mechanical_emergency_thread(void *data);
    static void* mechanical_demo_server_thread(void *);

    void init_network();
    void mechanic_control();
    void mechanical_emergency() const;

    void handle(Message *message);
    void start_conveyor(int which) const;
    void stop_conveyor(int which) const;
    void push_pusher(int which) const;
    void reset_mechanical_system() const;
};

#endif //INTELLI_MECHANIC_HPP
